package xray

const (
	PriorityMedium = "MEDIUM"
)

const (
	ExecutionSettingRuleAllow   = "ALLOW"
	ExecutionSettingTimeTypeDay = "DAY"
	PlanSettingPlanTypeDay      = "DAY"
	TaskTargetTypeManual        = "MANUAL"
)

type ExecutionSetting struct {
	Enabled  bool   `json:"enabled"`
	Rule     string `json:"rule"`
	TimeType string `json:"timeType"`
}

type PlanSetting struct {
	Enabled  bool   `json:"enabled"`
	PlanType string `json:"planType"`
	ExecTime string `json:"execTime"`
}

type TaskTarget struct {
	Target     []string `json:"target"`
	TargetType string   `json:"targetType"`
}

type BasicSetting struct {
	EngineChoice     []string         `json:"engineChoice"`
	ExecutionSetting ExecutionSetting `json:"executionSetting"`
	GlobalWhiteList  []interface{}    `json:"globalWhiteList"`
	PlanSetting      PlanSetting      `json:"planSetting"`
	Remark           string           `json:"remark"`
	TaskName         string           `json:"taskName"`
	TaskTarget       TaskTarget       `json:"taskTarget"`
	TemplateSync     bool             `json:"templateSync"`
}

type HTTPRequest struct {
	Proxy     string        `json:"proxy"`
	Cookies   []interface{} `json:"cookies"`
	Headers   []interface{} `json:"headers"`
	UserAgent string        `json:"userAgent"`
}

type ExtraOptions struct {
	ScanDepth              int  `json:"scanDepth"`
	MaxParallel            int  `json:"maxParallel"`
	OpenFingerBindingCheck bool `json:"openFingerBindingCheck"`
}

type FinishAutoReport struct {
	Enabled        bool `json:"enabled"`
	ReportTemplate int  `json:"reportTemplate"`
}

type TaskRestriction struct {
	Debug bool `json:"debug"`
	Email struct {
		Enabled bool `json:"enabled"`
	} `json:"email"`
	White struct {
		Enabled bool `json:"enabled"`
	} `json:"white"`
	BandWidth                     int              `json:"bandWidth"`
	UpdateVuln                    bool             `json:"updateVuln"`
	CreateAsset                   bool             `json:"createAsset"`
	MaxParallel                   int              `json:"maxParallel"`
	TaskTimeout                   int              `json:"taskTimeout"`
	UpdateAsset                   bool             `json:"updateAsset"`
	TransformDead                 bool             `json:"transformDead"`
	FinishAutoReport              FinishAutoReport `json:"finishAutoReport"`
	FixUnfoundVulnAfterScanFinish bool             `json:"fixUnfoundVulnAfterScanFinish"`
}

type PluginConfigDetail struct {
	BruteForceSetting struct {
		Enabled bool `json:"enabled"`
	} `json:"bruteForceSetting"`
	DirScanDictionary struct {
		Enabled bool `json:"enabled"`
	} `json:"dirScanDictionary"`
	WeakPasswordDictionary struct {
		Enabled bool `json:"enabled"`
	} `json:"weakPasswordDictionary"`
}

type BlindReversePlatform struct {
	Enabled bool `json:"enabled"`
}

type InfoCollection struct {
	Other struct {
		EnableDnsReverse               bool `json:"enableDnsReverse"`
		EnableLocalNetworkOptimization bool `json:"enableLocalNetworkOptimization"`
	} `json:"other"`
	TCPPort struct {
		Config struct {
			Timeout  int `json:"timeout"`
			MaxRetry int `json:"maxRetry"`
			MinRetry int `json:"minRetry"`
		} `json:"config"`
		Enabled           bool     `json:"enabled"`
		TCPPorts          []string `json:"TCPPorts"`
		ScanType          string   `json:"scanType"`
		DetectionLevel    string   `json:"detectionLevel"`
		EnableFingerprint bool     `json:"enableFingerprint"`
	} `json:"TCPPort"`
	UDPPort struct {
		Config struct {
			MaxQPS   int `json:"maxQPS"`
			Timeout  int `json:"timeout"`
			MaxRetry int `json:"maxRetry"`
			MinRetry int `json:"minRetry"`
		} `json:"config"`
		Enabled bool `json:"enabled"`
	} `json:"UDPPort"`
	HTTPRequest struct {
		Proxy     string        `json:"proxy"`
		Cookies   []interface{} `json:"cookies"`
		Headers   []interface{} `json:"headers"`
		UserAgent string        `json:"userAgent"`
	} `json:"HTTPRequest"`
	HostLoginSetting struct {
		Enabled bool `json:"enabled"`
	} `json:"HostLoginSetting"`
	HostLiveDetection struct {
		Enabled   bool `json:"enabled"`
		UDPConfig struct {
			Timeout  int `json:"timeout"`
			MaxRetry int `json:"maxRetry"`
			MinRetry int `json:"minRetry"`
		} `json:"UDPConfig"`
		ICMPConfig struct {
			Timeout  int `json:"timeout"`
			MaxRetry int `json:"maxRetry"`
			MinRetry int `json:"minRetry"`
		} `json:"ICMPConfig"`
		TCPSYNConfig struct {
			Timeout  int `json:"timeout"`
			MaxRetry int `json:"maxRetry"`
			MinRetry int `json:"minRetry"`
		} `json:"TCPSYNConfig"`
		SkipDownHost     bool `json:"skipDownHost"`
		TCPConnectConfig struct {
			MaxQPS        int `json:"maxQPS"`
			Timeout       int `json:"timeout"`
			MaxRetry      int `json:"maxRetry"`
			MinRetry      int `json:"minRetry"`
			MaxConcurrent int `json:"maxConcurrent"`
		} `json:"TCPConnectConfig"`
		HostLiveScanType []string `json:"hostLiveScanType"`
	} `json:"hostLiveDetection"`
}

type TaskSetting struct {
	TCPPorts                       []string             `json:"TCPPorts"`
	UDPPorts                       []string             `json:"UDPPorts"`
	HTTPRequest                    HTTPRequest          `json:"HTTPRequest,omitempty"`
	ExtraOptions                   ExtraOptions         `json:"extraOptions"`
	SkipDownHost                   bool                 `json:"skipDownHost"`
	PluginSetting                  []interface{}        `json:"pluginSetting"`
	InfoCollection                 InfoCollection       `json:"infoCollection,omitempty"`
	EnableVulnScan                 bool                 `json:"enableVulnScan"`
	TaskRestriction                TaskRestriction      `json:"taskRestriction"`
	PluginConfigDetail             PluginConfigDetail   `json:"pluginConfigDetail"`
	BlindReversePlatform           BlindReversePlatform `json:"blindReversePlatform"`
	CustomFingerprintPluginSetting []interface{}        `json:"customFingerprintPluginSetting"`
}

func NewSimpleBasicSetting(taskName, reMark string, target []string, engineIds []string) BasicSetting {
	return BasicSetting{
		EngineChoice: engineIds,
		ExecutionSetting: ExecutionSetting{
			Enabled:  false,
			Rule:     ExecutionSettingRuleAllow,
			TimeType: ExecutionSettingTimeTypeDay,
		},
		PlanSetting: PlanSetting{
			Enabled:  true,
			PlanType: PlanSettingPlanTypeDay,
			ExecTime: "22:22:22+08:00",
		},
		Remark:   reMark,
		TaskName: taskName,
		TaskTarget: TaskTarget{
			Target:     target,
			TargetType: TaskTargetTypeManual,
		},
		TemplateSync: false,
	}
}

func NewSimpleTaskSetting(tcpPorts, udpPorts []string, enableVulnScan bool) TaskSetting {
	return TaskSetting{
		TCPPorts:       tcpPorts,
		UDPPorts:       udpPorts,
		HTTPRequest:    HTTPRequest{UserAgent: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"},
		ExtraOptions:   ExtraOptions{ScanDepth: 2, MaxParallel: 30},
		SkipDownHost:   true,
		EnableVulnScan: enableVulnScan,
		TaskRestriction: TaskRestriction{
			Debug:         false,
			BandWidth:     2000,
			UpdateVuln:    false,
			CreateAsset:   true,
			MaxParallel:   50,
			TaskTimeout:   1800,
			UpdateAsset:   true,
			TransformDead: false,
			FinishAutoReport: FinishAutoReport{
				Enabled:        false,
				ReportTemplate: 2,
			},
		},
		PluginSetting: []interface{}{},
		PluginConfigDetail: PluginConfigDetail{
			DirScanDictionary: struct {
				Enabled bool `json:"enabled"`
			}{false},
			WeakPasswordDictionary: struct {
				Enabled bool `json:"enabled"`
			}{false},
		},
		BlindReversePlatform: BlindReversePlatform{Enabled: false},
	}
}
